﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Telerik.Web.UI;
using HandBook.Data;
using HandBook.Common;
using System.Text.RegularExpressions;

public partial class Admin_FoodBookDetails_Admin : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["FoodBookID"] == null)
            {
                DetailsView1.DefaultMode = DetailsViewMode.Insert;
                Button3.Text = "Insert";
            }
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        if(Button3.Text == "Insert") 
        {
            InsertFoodBook();
            ScriptManager.RegisterStartupScript(this, GetType(), "closeWnd", "CloseWnd();", true);
        }
    }
    private void InsertFoodBook() 
    {
        FoodBook food = new FoodBook();
        food.FoodStyleID = Convert.ToInt32((DetailsView1.FindControl("ddlFoodStyleID") as DropDownList).SelectedValue);        
        food.Date = Convert.ToDateTime(DateTime.Now.ToString());
        food.Title = (DetailsView1.FindControl("txtTitle") as TextBox).Text.Trim();
        food.ShortDescription = (DetailsView1.FindControl("RadEditorShort") as RadEditor).Content.Trim();
        string nguyenLieu = "<b>" + "Nguyên Liệu :" + "</b>" + "</br>" + (DetailsView1.FindControl("RadEditorNguyenLieuInsert") as RadEditor).Content.Trim();
        string thucHien = "<b>" + "Thực Hiện :" + "</b>" + "</br>" + (DetailsView1.FindControl("RadEditorThucHienInsert") as RadEditor).Content.Trim();
        food.LongDescription = "-->" + nguyenLieu.Trim() + "-->" + thucHien.Trim();
        string fileUpload = (DetailsView1.FindControl("FileUpload1") as FileUpload).FileName.Trim();
        if (!fileUpload.Equals(""))
        {
            string file = HandBook.Common.FileManager.GetFileName(fileUpload);
            string path = Server.MapPath("~/Images/Foods/" + file);
            (DetailsView1.FindControl("FileUpload1") as FileUpload).SaveAs(path);
            food.Image = file;
        }
        food.Path = (DetailsView1.FindControl("txtPath") as TextBox).Text.Trim();
        food.IsShowHomePage = Convert.ToBoolean("False");
        HandBookData.EatDrinkDB.FoodBooks.InsertOnSubmit(food);
        HandBookData.EatDrinkDB.SubmitChanges();
    }
}
